using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Api.Demo.Data;
using Microsoft.EntityFrameworkCore;
using System.Reflection;
using System.IO;

namespace Api.Demo
{

    public class Startup
    {

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public IConfiguration Configuration { get; }

        /// This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllers();
            services.AddDbContext<DbContextDemo>(option =>
            {
                option.UseSqlServer(Configuration.GetConnectionString("conStr"));
            });

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1.0.0",//版本号
                    Title = "阎罗殿生死簿API",//标题
                    Description = "关于阎罗殿生死簿的相关api",//描述
                    TermsOfService = new Uri("https://xiatianyu.cn"),//服务条款
                    Contact = new OpenApiContact
                    {
                        Name = "小鬼",//联系人
                        Email = "mxie7122@gmail.com",//邮箱地址
                        Url = new Uri("https://cxff.xiatianyu.cn"),//网址
                    },
                    License = new OpenApiLicense
                    {
                        Name = "MIT",//协议
                        Url = new Uri("https://xiatianyu.cn")//协议网址
                    }
                });
                // 获取xml文件名
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                // 获取xml文件路径
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                // 添加控制器层注释，true表示显示控制器注释
                c.IncludeXmlComments(xmlPath, true);
            });
        }

        /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Api.Demo v1"));
            }

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
